package com.easidea.tim.flex.remote.system
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.bean.system.Department;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	public class DepartmentDao extends BaseDao
	{
		
		private var _service:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:DepartmentDao 
		[Bindable] private var currentDeptId : int;
		
		
		/**
		 * Singleton
		 */
		public static function getInstance() : DepartmentDao {
			if ( _instance == null ){
				_instance = new DepartmentDao();
			}
			return _instance;
		}
		
		function DepartmentDao(){
			_service.channelSet = this.buildChannelSet();
			_service.destination = "departmentService";
			_service.concurrency = "last";
			_service.makeObjectsBindable = true;
			_service.showBusyCursor = true;
		}
		
		
		/***************** 业务方法 ********************/
		
		/**
		 * 保存部门信息
		 */
		public function saveDepartment(dept : Department):void {
			_service.addEventListener(ResultEvent.RESULT,processSaveDeptEvent);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.saveDept(dept);
		}
		
		private function processSaveDeptEvent(event: ResultEvent):void {
			_service.removeEventListener(ResultEvent.RESULT, processSaveDeptEvent);
			var message : String;
			if(event.result as String == "-2"){
				// 保存二级数据失败，父对象找不到
				message = "保存失败，未找到指定数据！";
			}else if(event.result as String == "0"){
				// 保存二级数据失败，父对象找不到
				message = "保存失败，空数据！";
			}else if(event.result as String == "2"){
				message = "数据修改成功！";
			}else {
				message = "保存成功！";
			}
			AppAlert.show(message,"提示");
			Env.dispatchEvent(new CustomerEvent("saveDeptEvent",event.result));	
		}
		
		/**
		 * 查询所有数据
		 */
		public function queryAll() : void {
			_service.addEventListener(ResultEvent.RESULT, processQueryAllEvent);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.getAllDepartmentList(); // 调用spring service对应方法
		}
		
		private function processQueryAllEvent(event:ResultEvent) : void {
			_service.removeEventListener(ResultEvent.RESULT, processQueryAllEvent);
			if(event != null && event.result != null) {
				var resultArray : ArrayCollection = event.result as ArrayCollection;
				//trace(resultArray);
				Env.dispatchEvent(new CustomerEvent("queryAllEvent",resultArray));
			}
		}
		
		/**
		 * 删除数据
		 */
		public function deleteDept (id : int, modifier: String) : void {
			if(id != -1 && id !=0){
				_service.addEventListener(ResultEvent.RESULT, processDelDeptEvent);
				_service.addEventListener(FaultEvent.FAULT, _Fault);
				
				var delDept :Department = new Department();
				delDept.id = id;
				currentDeptId = id;
				delDept.deleted = 1;
				delDept.modifier = modifier;
				
				_service.delDept(delDept);
			}
		}
		
		private function processDelDeptEvent(event: ResultEvent) : void {
			_service.removeEventListener(ResultEvent.RESULT, processDelDeptEvent);
			switch (event.result as int){
				case 0: // success
					AppAlert.show("删除成功！","提示");
					Env.dispatchEvent(new CustomerEvent("delDeptEvent",true,currentDeptId));
					break;
				case -1: // null parameter
					AppAlert.show("删除失败，系统异常！","提示");
					Env.dispatchEvent(new CustomerEvent("delDeptEvent",false));
					break;
				case -2: // 找不到ID
					AppAlert.show("删除失败，指定数据不存在！","提示");
					Env.dispatchEvent(new CustomerEvent("delDeptEvent",false));
					break;
				case -3: // 有子对象
					AppAlert.show("删除失败，指定数据有次级部门，不能删除！","提示");
					Env.dispatchEvent(new CustomerEvent("delDeptEvent",false));
					break;
			}
		}
	}
}